<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Login extends Controller
{
    public function index()
    {
        // 读取用户列表
        // return $this->fetch('/admin/view/Login/index.html');

        return view('admin@Login/index',[
            'title' => '我是显示登录页面'
        ]);
    }

      // 处理登录逻辑
    public function doLogin()
    {
        // session_start();
        //接收用户名和密码
        $rev = request()->post();
        // var_dump($rev);die;
        $username = $rev['username'];
        $password = $rev['pwd'];

        //验证
        // $user = M('a_user');
        $data = Db::name('a_user')->where('username',$username)->select();
        if (!$data) {
            $this->error('用户名不存在！');
            exit;
        }
        //验证密码
        if ($data[0]['userpass'] != md5($password)) {
            $this->error('密码不正确');
            exit;
        }
        // var_dump($data);die;
        //把用户信息添加到session
        // $_SESSION['username'] = $data;
        Session::set('username',$data);
        // var_dump($_SESSION);die;
        // var_dump(555);die;
        $tj = Db::name('a_count')->order('id','desc')->limit(7)->select();

        //根据用户id获取对应的节点信息
        //$sql = "select n.mname,n.aname from lamp_user u join lamp_user_role ur on u.id=ur.uid join lamp_role_node rn on ur.rid=rn.rid join lamp_node n on rn.nid=n.id where u.id={$users['id']}";
        //$list = M()->query($sql);

        $res = Db::name('a_user')->field('id')->where('username',$username)->find();
        $id = $res['id'];

        $list = Db::table('sm_a_user')
                ->alias('sau')
                ->join('sm_a_user_role saur', 'saur.uid  = sau.id')
                ->join('sm_a_role sar', 'sar.id = saur.rid')
                ->join('sm_a_role_power sarp', 'sarp.rid  = sar.id')
                ->join('sm_a_power sap', 'sap.id = sarp.pid')
                ->where('sm_a_user.id',$id)
                ->field('sap.mname,sap.aname')
                ->select();
        // var_dump($list);die;


        //控制器名转换为大写
        foreach ($list as $key => $val) {
            $list[$key]['mname'] = ucfirst($val['mname']);
        }
        //查看查询出来的信息
        // V($list); exit;

        $nodelist = array();
        $nodelist['Index'] = array('index');
        //遍历重新拼装
        foreach($list as $v){
            $nodelist[$v['mname']][] = $v['aname'];
            //把修改和执行修改 添加和执行添加 拼装到一起
            if($v['aname']=="edit"){
                $nodelist[$v['mname']][]="update";
            }
            if($v['aname']=="add"){
                $nodelist[$v['mname']][]="insert";
            }
        }

        //将权限信息放置到session中
        $_SESSION['think']['username']['nodelist'] = $nodelist;
        // Session::set('username',$nodelist);
        // var_dump($_SESSION['username']);die;
        //重组的信息
        // V($_SESSION);exit;
        // var_dump($_SESSION['think']);die;
        //跳转到首页
        // $this->redirect('Index/index');
        // 总注册用户
        $ap = Db('h_user')
              ->count('id');

        // 今天新增用户
        $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $tp = Db('h_user')
                ->where('createtime','>',$beginToday)
                ->where('createtime','<',$endToday)
                ->count('id');
        // 总片源
         $am = Db('h_movie')
                ->count('id');
        // 今日评论数
        $tc = Db('h_comment')
                ->where('time','>',date('Y-m-s h:i:s',$beginToday))
                ->where('time','<',date('Y-m-s h:i:s',$endToday))
                ->count('id');
                
        // 计算总注册用户（昨日）
        
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

        $yp = Db('h_user')
            ->where('createtime','>',$beginYesterday)
            ->where('createtime','<',$endYesterday)
            ->count('id');
        $aip = $ap - $yp;
        $aipl = number_format(($aip / $ap *100), 2);

        // 昨日新增用户
        $yip = Db('h_user')
                ->where('createtime','>',$beginYesterday)
                ->where('createtime','<',$endYesterday)
                ->count('id');
        $ip = $tp - $yip;
        if($tp == 0){
            $ipl = 0;
        }else{
            $ipl = number_format(($ip / $tp *100), 2);
        }

        // 昨日总片源
        $ym = Db('h_movie')
                ->where('myear','<',date('Y-m-s',$endYesterday))
                ->count('id');
        $im = $am - $ym;
        if($am == 0){
            $aim = 0;
        }
        $aim = number_format(($im / $am *100), 2);

        // 昨日评论数
        $yc = Db('h_comment')
                ->where('time','>',date('Y-m-s h:i:s',$beginYesterday))
                ->where('time','<',date('Y-m-s h:i:s',$endYesterday))
                ->count('id');
        $ic = $tc - $yc;
        if($tc == 0){
            $aic = 0;
        }
        $aic = number_format(($ic / $tc *100), 2);

        return view('admin@AdminController/index', [
                'ap' => $ap,
                'tp' => $tp,
                'am' => $am,
                'tc' => $tc,
                'aipl' => $aipl,
                'ipl' => $ipl,
                'aim' => $aim,
                'aic' => $aic,
                'tj' => $tj,
            ]);
    }

}